package com.erpanderp.wam.usm.model.service.timesheet;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.usm.model.entity.timesheet.Timesheet;
import com.erpanderp.wam.usm.model.entity.timesheet.TimesheetDetail;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.HibernateUtil;

public class TimesheetDetailDAO {

	public TimesheetDetail updateTimesheetDetail(TimesheetDetail timesheetDetail) {
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.COMMIT);
			session.saveOrUpdate(timesheetDetail);
			session.getTransaction().commit();
			return timesheetDetail;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
    
	public boolean updateTimesheetDetails(List<TimesheetDetail> timesheetDetailsList) {
		
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.COMMIT);
			for(TimesheetDetail timesheetDetail : timesheetDetailsList){
			 session.update(timesheetDetail);
			}
			
			session.getTransaction().commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	

	
	
	public List<TimesheetDetail> getTimesheetDetails(Long timesheetId){
		List<TimesheetDetail> timesheetDetails = null;
		
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from TimesheetDetail td where td.timesheet.timesheetId = :timesheetId";
			Query query = session.createQuery(queryString);
			query.setLong("timesheetId", timesheetId);
			timesheetDetails = (List<TimesheetDetail>) query.list();
			session.getTransaction().commit();
			return timesheetDetails;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public List<TimesheetDetail> getTimesheetDetails(Date fromDate, Date toDate, Long employeeId){
		List<TimesheetDetail> timesheetDetails = null;
		
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from TimesheetDetail td where td.timesheet.employee.employeeId = :employeeId" +
								" and (td.timesheetDate >= :fromDate and td.timesheetDate <= :toDate)";
			Query query = session.createQuery(queryString);
			query.setLong("employeeId", employeeId).setDate("fromDate", fromDate).setDate("toDate", toDate);
			timesheetDetails = (List<TimesheetDetail>) query.list();
			session.getTransaction().commit();
			return timesheetDetails;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public TimesheetDetail getTimesheetDetail(Date date, Long employeeId){
		
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from TimesheetDetail td where td.timesheet.employee.employeeId = :employeeId" +
								" and td.timesheetDate = :date";
			Query query = session.createQuery(queryString);
			query.setLong("employeeId", employeeId).setDate("date", date);
			List<TimesheetDetail> temp = (List<TimesheetDetail>) query.list();
			session.getTransaction().commit();
			if(temp != null && temp.size() > 0){
				return temp.get(0);
			}
			
			return null;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public List<TimesheetDetail> getTimesheetDetails(Date date){
		
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from TimesheetDetail td where td.timesheetDate = :date and td.timesheet.employee.status =:status";
			Query query = session.createQuery(queryString);
			query.setDate("date", date).setString("status", CommonLookup.STATUS_ACTIVE);
			List<TimesheetDetail> temp = (List<TimesheetDetail>) query.list();
			session.getTransaction().commit();
			return temp;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public List<TimesheetDetail> getApprovedTimesheetDetails(Date fromDate, Date toDate, Long employeeId){
		List<TimesheetDetail> timesheetDetails = null;
		
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from TimesheetDetail td where td.timesheet.employee.employeeId = :employeeId" +
								" and td.timesheetDate >= :fromDate and td.timesheetDate <= :toDate" +
								" and td.timesheet.timesheetStatus = :timesheetStatus";
			Query query = session.createQuery(queryString);
			query.setLong("employeeId", employeeId).setDate("fromDate", fromDate).setDate("toDate", toDate).setString("timesheetStatus", CommonLookup.TIMESHEET_STATUS_APPROVED);
			timesheetDetails = (List<TimesheetDetail>) query.list();
			session.getTransaction().commit();
			return timesheetDetails;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public TimesheetDetail getTimesheetDetail(Long timesheetDetailId){
		TimesheetDetail timesheetDetail = null;
		
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			session.setFlushMode(FlushMode.COMMIT);
			timesheetDetail = (TimesheetDetail) session.load(TimesheetDetail.class, timesheetDetailId);
			session.getTransaction().commit();
			return timesheetDetail;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public static void main(String a[]){
		System.out.println(new GregorianCalendar(2012, 5, 28).getTime());
		System.out.println(new TimesheetDetailDAO().getTimesheetDetails(new GregorianCalendar(2012, 5, 15, 0, 0, 0).getTime(), new GregorianCalendar(2012, 5, 15, 0, 0,0).getTime(), 45l));
		
	}
}
